package goodbaby.dklt.customerview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * Created by Li.z on 2016/4/30.
 * 七天步数表格
 */
public class HistoryFormView extends View{
    private int mWidth;
    private int mHeight;

    private int mRectWidth;//小方块的宽
    private int mRectHeight;//小方块的高

    private int mHSpace;//方块之间的水平距离
    private int mVSpace;//方块之间的垂直距离

    private int mBottom;//最下面的方块纵坐标

    private Paint rectPaint; // 方块
    private Paint textPaint;
    private Paint dotPaint;
    private Paint textTopPaint;

    private Path mPath = new Path();

    private int rectColorBlue = Color.parseColor("#25b9f2");
    private int rectColorGray = Color.parseColor("#dedede");

    private String rightString="- -";
    private String leftString="- -";

    private List<HistoryData> listData;
    private int average;//平均值

    private int currentIndex = 1;

    public HistoryFormView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        initCofig(context, attrs);
    }

    public HistoryFormView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initCofig(context, attrs);
    }

    private void initCofig(Context context, AttributeSet attrs) {
        textPaint = new Paint();
        textPaint.setTextSize(20);
        textPaint.setColor(rectColorGray);
        textPaint.setTextAlign(Paint.Align.CENTER);

        textTopPaint = new Paint();
        textTopPaint.setTextSize(20);
        textTopPaint.setColor(rectColorGray);
        textTopPaint.setTextAlign(Paint.Align.LEFT);

        rectPaint = new Paint();
        rectPaint.setColor(rectColorBlue);

        dotPaint = new Paint();
        dotPaint.setStyle(Paint.Style.STROKE);
        dotPaint.setColor(rectColorGray);
        PathEffect effects = new DashPathEffect(new float[]{5,5,5,5},1);
        dotPaint.setPathEffect(effects);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mWidth = MeasureSpec.makeMeasureSpec(widthMeasureSpec,MeasureSpec.getMode(widthMeasureSpec));
        mWidth = MeasureSpec.getSize(mWidth);
        mHeight = (int)(mWidth*0.2187f);

        mRectWidth = (int)(mWidth*0.0432f);
        mRectHeight = (int)(mRectWidth*0.333f);

        textPaint.setTextSize(mRectHeight*2.5f);
        textTopPaint.setTextSize(textPaint.getTextSize());

        mVSpace = (int)(mRectHeight*0.2f);
        mHSpace = (mWidth - mRectWidth*9)/6;

        mBottom = (int)(mWidth*0.04f);

        setMeasuredDimension(mWidth,mHeight);
        Log.i("ViewonMeasure",mWidth+","+mHeight);
        Log.i("ViewonMeasure",mRectWidth+","+mRectHeight+","+mVSpace+","+mHSpace+","+mBottom);
    }

    private HistoryData tempData;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //画左边文字
        canvas.drawText(leftString,0,textPaint.getTextSize(),textTopPaint);


        //画右边文字
        canvas.drawText(rightString,mWidth-textTopPaint.measureText(rightString)-5,textPaint.getTextSize(),textTopPaint);

        //Random random = new Random();

        if(listData!=null){
            //画表格
            for(int i = 0 ; i < 7 && i<listData.size() ; i++){
                tempData = listData.get(i);
                //画文字
                canvas.drawText(tempData.txt,mRectWidth+0.5f*mRectWidth+i*(mRectWidth+mHSpace),mHeight,textPaint);
                //画方块
                int left = 0;
                int top = 0;
                int right = 0;
                int bottom = 0;
                int max = tempData.number;
                for(int  j= 0 ; j < max && j<currentIndex; j++){
                    left = mRectWidth + i*(mRectWidth+mHSpace);
                    right= left + mRectWidth;
                    bottom = mHeight - mBottom -mRectHeight*j - mVSpace*j;
                    top = bottom - mRectHeight;

                    if(max<average){
                        rectPaint.setColor(rectColorGray);
                    }else{
                        rectPaint.setColor(rectColorBlue);
                    }
                    canvas.drawRect(left,top,right,bottom,rectPaint);
                }
                //画虚线
                int n = average;
                int y = mHeight - mBottom -n*mRectHeight - (n-1)*mVSpace;
                mPath.moveTo(0, y);
                mPath.lineTo(mWidth, y);
                canvas.drawPath(mPath,dotPaint);
            }
        }
    }

    public HistoryFormView setLeftString(String str){
        leftString = str;
        return this;
    }

    public HistoryFormView setRightString(String str){
        rightString = str;
        return this;
    }

    public HistoryFormView setHistoryData(DataAdapter adapter){
        listData = adapter.getData();
        int total = 0;
        for (HistoryData data : listData){
            total+=data.number;
        }
        average = total/7;
        return this;
    }

    public void setAverage(int i){
        average = i;
    }

    public int getAverage(){
        return average;
    }

    public void show(){
        currentIndex = 1;
        ValueAnimator animator = ValueAnimator.ofInt(1,8);
        //animator.setInterpolator(new DecelerateInterpolator());
        animator.setDuration(1300);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                currentIndex= (int) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.start();
    }

    public interface DataAdapter{
        List<HistoryData> getData();
    }

    public static class HistoryData{
        public int number;//最大值8
        public String txt;
    }

    public static String getToday(String dateStr){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(dateStr);
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            return calendar.get(Calendar.DAY_OF_MONTH)+"日";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "-";
    }


}
